// pages/sign/sign.js

const app = getApp();
Page({

  /**
   *  页面的初始数据
   */
  data: {
    userInfo: {},
    parentId: "",
    pageUrl: "",
    imgUrl: null
   
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      parentId: options.parentId,
      pageUrl: decodeURIComponent(options.pageUrl),
    });

    wx.request({
      url: app.globalData.apiurl + '/member/api/MallBaseSet/GetMallBaseSetByCustID',
      method: 'get',
      header: {
        'X_ManuName': app.globalData.manuName
      },
      data: {
        CustId: app.globalData.custID
      },
      success: res => {
        var data = res.data;
        if (data.code == 1200 && data.content) {
        
          wx.setNavigationBarTitle({
            title: data.content.MallName,
          })
          var imgUrl = "/image/wxhy.png";
          if (data.content.Logo != null) {
            imgUrl = app.globalData.imgurl+data.content.Logo;
          }
          that.setData({
            imgUrl: imgUrl,
          });
        }

      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  registUser: function(userRes, session_key) {
    var that = this;
    wx.request({
      url: app.globalData.apiurl + '/member/api/Register/DecodeUserInfo',
      method: 'post',
      header: {
        'X_ManuName': app.globalData.manuName
      },
      data: {
        signature: userRes.signature,
        encryptedData: userRes.encryptedData,
        iv: userRes.iv,
        rawData: userRes.rawData,
        session_key: session_key,
        custid: app.globalData.custID,
        parentId: that.data.parentId
      
      },
      success: function(res) {
        if (res.data.code == 1200 || res.data.code == 1300) {
          wx.setStorageSync('openId', res.data.content.OpenID)
          wx.setStorageSync('unionId', res.data.content.Unionid)

          wx.setStorageSync('UserInfo', res.data.content);
          if (res.data.code == 1300) {
            wx.setStorageSync('IsBindMobile', "Mobile"); //是否绑定手机
            wx.redirectTo({
              url: '/pages/register/register?pageUrl=' + encodeURIComponent(that.data.pageUrl)
            })
          } else {
            wx.reLaunch({
              url: that.data.pageUrl
            })
          }


        } else {
          wx.showModal({
            content: res.data.content == null ? "解密出错!" : res.data.content,
            showCancel: false,
            confirmText: "确定"
          })
        }

      },
      fail(res) {
        wx.showModal({
          content: "服务器请求失败!",
          showCancel: false,
          confirmText: "确定"
        })

      }
    });


  },
  /**
   * 获取sessionKey, failCB
   */
  getSessionKey: function(successCB) {


    wx.login({
      success: function(resCode) {
        var postData = null;
        var url = "";
        if (app.globalData.isAuthComponent) {
          url = app.globalData.apiurl + '/member/api/Small/ComponentJsCode2Json';
          postData = {
            zpAppId: app.globalData.zhengpinAppid,
            jsCode: resCode.code
          };
        } else {
          url = app.globalData.apiurl + '/member/api/Small/JsCode2JsonNew';
          postData = {
            jsCode: resCode.code,
            appId: app.globalData.zpAppid,
            secret: app.globalData.secret,
            grant_type: 'authorization_code',
          }
        }
        wx.request({
          url: url,
          method: "get",
          data: postData,
          header: {
            'X_ManuName': app.globalData.manuName,
          },
          success: res => { //成功
            var data = res.data;

            if (res.statusCode == 200 && data.code == 1200 && data.content.session_key) {
              successCB(data.content.session_key);
            } else {
              wx.showModal({
                content: "请求getSessionKey出错!",
                showCancel: false,
                confirmText: "确定"
              })
              // failCB();
            }
          },
          fail: res => {
            wx.showModal({
              content: "网络请求getSessionKey失败!",
              showCancel: false,
              confirmText: "确定"
            })
            // failCB();
          }
        })
      },
      fail: res => {
        wx.showModal({
          content: "登陆失败",
          showCancel: false,
          confirmText: "确定"
        })
        // failCB();
      }
    })
  },


  getUserInfo: function(e) {
    var that = this;
    if (e.detail.encryptedData) //授权成功
    {
      that.getSessionKey(function(res) {
        that.registUser(e.detail, res);
      })

    } else {
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法正常使用功能体验。请重新授权。',
        showCancel: false,
      });
    }

  }
})